Skip to content

perf(ivf): route batched Flat queries with blocked top-nprobe assignment - #264

Closed
ColinLeeo wants to merge 1 commit into
VectorDB-NTU:mainfrom
ColinLeeo:codex/ivf-batched-flat-routing
Closed

ColinLeeo wants to merge 1 commit into
VectorDB-NTU:mainfrom
ColinLeeo:codex/ivf-batched-flat-routing

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Nov 24, 2026 •

Copy link
Copy Markdown

Superseded by #265 after renaming the source branch to perf/ivf-batched-flat-routing. The implementation commit and validation results are unchanged.

Summary

Addresses #218. Flat IVF batch search currently rereads every centroid for each query. This change shares centroid reads across query blocks using GEMM screening and a bounded top-nprobe heap, then recomputes retained candidates with the existing float distance kernels.

  • Compute centroid norms once per call and reuse per-worker scratch. Stream query blocks through rotation, routing and scanning; use 64-row parallel blocks and up to 512 rows for serial NEON, with buffer-budget-based sizing.
  • Limit GEMM to one thread inside each OpenMP worker, including the single-worker case, without changing the caller's thread settings.
  • Order both scalar and batched routing by the returned float distance, then centroid ID. This explicitly resolves floating-point ties, including different squared L2 distances that round to the same float after sqrt.
  • Keep the existing per-query path for single/small batches, fewer than 256 centroids, and nprobe above one quarter of the cluster count. The blocked path requires at least 32 queries per worker.
  • Preserve cosine query normalization, including the original-space query needed for raw-vector reranking. No public API, dependency, or saved-index format changes.

Adds C++ and Python regressions for centroid/query-block boundaries, duplicated centroids, L2/IP/cosine, zero-valued queries, 1/5/32-bit storage, both FastScan accuracy modes, removed points, oversized k/nprobe, optional distances, thread-setting preservation and save/load. Adds an IVF search benchmark driver and its usage documentation.

This PR contains only the batch-routing change; no FastScan kernel optimization is included.

Testing

Based on main 9121a79ae0f5e23d3c5f5aef2431e872b89afd92 (0.9.0). Native Apple M5 Pro ARM64, AppleClang 17.0.0.17000604, Release -O3, NEON, RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF, OpenMP enabled.

  • CMake build and full CTest: 629 passed, 14 skipped, 0 failed (643 registered tests). Focused routing/batch/module-boundary tests: 5 passed.
  • Rebuilt 0.9.0 Python wheel with the existing Python 3.11 project environment, installed into an isolated target directory, and verified the imported package/extension paths. Full Python suite: 1188 passed, 42 skipped, 0 failed; IVF suite: 108 passed, 3 skipped. Skips include platform-specific checks, optional example executables and wheel-packaging checks. Four non-UTF-8 pathname probes initially hit sandbox restrictions; the suite was rerun outside the sandbox and those tests correctly skipped because this filesystem rejects those names.
  • ./scripts/check-format.sh: clang-format 23.1.5 passed. ./scripts/check-python.sh: Ruff 0.16.1 passed. git diff --check passed.
  • Full ./scripts/check-tidy.sh build-wheel: clang-tidy 23.1.4 passed across all 40 configured production/binding translation units, with matching LLVM 23 resource headers for analysis. A separate focused run on the new C++ routing test also passed.
  • Native Linux/x86-64 runtime and performance have not been tested locally for this revision. Cross-platform CI remains to be run.
Build and test commands

python below is the existing project virtual-environment interpreter. GoogleTest 1.14.0 was reused from the existing local source cache; unused GoogleMock was disabled because a system-installed GoogleTest header interfered with its build.

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DRABITQ_BUILD_TESTS=ON -DRABITQ_BUILD_SAMPLES=OFF \
  -DRABITQ_BUILD_BENCHMARKS=ON -DRABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF \
  -DOpenMP_ROOT="$(brew --prefix libomp)" -DBUILD_GMOCK=OFF \
  -DFETCHCONTENT_SOURCE_DIR_GOOGLETEST="$GOOGLETEST_SOURCE"
cmake --build build --parallel 6
OMP_NUM_THREADS=2 ctest --no-tests=error --test-dir build --output-on-failure
python -m pip wheel . --no-deps --no-build-isolation -w build/wheels \
  -Ccmake.define.OpenMP_ROOT="$(brew --prefix libomp)" \
  -Ccmake.define.RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF -Cbuild-dir=build-wheel
python -m pip install --no-deps --no-index --target build/python build/wheels/*.whl
OMP_NUM_THREADS=2 PYTHONPATH="$PWD/build/python" python -m pytest tests/python -q -rs
./scripts/check-format.sh
./scripts/check-python.sh
./scripts/check-tidy.sh build-wheel

Performance

GIST1M, 1,000,000 points × 960 dimensions; 4,096 Flat centroids; 5 bits; L2; k=10; default high-accuracy scanning. Both executables load the same prebuilt index and rotation, created with baseline 2122ab2: a 262,144-point training sample drawn without replacement using NumPy default_rng(42), seed 42, 10 iterations, construction with 8 threads. This isolates search changes from training/version differences; the current main and candidate both read this legacy index.

Compare current main against this PR using the same driver/compiler/options. Normally 1,000 queries, three alternating before/after process pairs, three timed repetitions per process after warmup; report median per-query time and its reciprocal QPS. The 4095/4096-probe cases use the first 64 queries and two pairs × two repetitions. Loading, warmup, hashing and recall calculation are outside timing. IDs and distance bytes have matching fingerprints in every comparison, and Recall@10 is unchanged (zero allowed quality loss).

Batch nprobe Threads Before µs/query After µs/query Before QPS After QPS Speedup Recall@10 (both)
1 64 1 826.04 862.48 1211 1159 0.958× 0.8832
8 64 1 774.14 818.91 1292 1221 0.945× 0.8832
64 64 1 835.79 697.30 1196 1434 1.199× 0.8832
1000 64 1 878.68 668.00 1138 1497 1.315× 0.8832
1000 16 1 442.51 261.00 2260 3831 1.695× 0.6598
1000 128 1 1300.76 1196.64 769 836 1.087× 0.9437
64 64 4 262.46 259.25 3810 3857 1.012× 0.8832
1000 64 4 254.70 206.67 3926 4839 1.232× 0.8832
64 4095 1 19190.56 18444.07 52 54 1.040× 0.9750
64 4096 1 19845.13 19345.20 50 52 1.026× 0.9750

Shared-host limitation: no CPU affinity on macOS; an unrelated VM was using approximately four CPU cores and the observed 1-minute load average was about 9.8. Small timing changes are not reliable evidence of a gain or regression.

The first sweep showed slower single/small-batch medians, so those cases were repeated with five alternating pairs × five repetitions, using the same binaries and data:

Batch nprobe Threads Before µs/query After µs/query Before QPS After QPS Speedup Recall@10 (both)
1 64 1 821.43 831.03 1217 1203 0.988× 0.8832
8 64 1 789.11 761.71 1267 1313 1.036× 0.8832

The single-query recheck is 1.2% slower by the aggregate median; per-pair speedups range from 0.971× to 1.088× (median 0.996×). The batch-8 aggregate changes direction between sweeps. These measurements do not establish a stable single-query regression or prove zero regression. Keeping this as a draft for review and an idle-host/x86 performance confirmation before merging; batch gains should also be remeasured on the target CPU.

Benchmark reproduction

Build the new benchmark driver against both revisions without changing the baseline search implementation. Use the same gist.index, query file and ground truth for both:

python benchmarks/hotpaths/compare.py \
  --before "$BEFORE gist.index gist_query.fvecs gist_groundtruth.ivecs 1000 64 1 3 1000" \
  --after  "$AFTER  gist.index gist_query.fvecs gist_groundtruth.ivecs 1000 64 1 3 1000" \
  --pairs 3 --output results-gist-b1000-p64-t1 \
  --notes "main 9121a79 vs this PR; GIST1M L2, 4096 Flat clusters, 5 bits, k10; record CPU/compiler/ISA/affinity"

Sweep the batch/probe/thread combinations in the table. For near-all-cluster searches use 64 4095 1 2 64 and 64 4096 1 2 64 with --pairs 2. For the longer recheck use batch 1 or 8, nprobe 64, one thread, five repetitions and --pairs 5.

@github-actions github-actions Bot added the release:performance Release category managed from the PR title label Nov 24, 2026
@ColinLeeo ColinLeeo closed this Nov 24, 2026
@ColinLeeo
ColinLeeo deleted the codex/ivf-batched-flat-routing branch November 24, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:performance Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant